var WorkFlowAssigneeManagerView = function(cfg) {
    return this.setup(cfg);
};
WorkFlowAssigneeManagerView.prototype.setup = function(cfg) {
    var store = this.initData();
    var checkboxSelectionModel = new Ext.grid.CheckboxSelectionModel();
    var columnModel = new Ext.grid.ColumnModel(
            {
                columns : [
                        checkboxSelectionModel,
                        {
                            header : "id",
                            dataIndex : "id",
                            hidden : true
                        },{
                            header : "公司名称",
                            dataIndex : "companyName",
                            sortable : true,
                            width : 80
                        },{
                            header : "部门名称",
                            sortable : true,
                            dataIndex : "departmentName",
                            width : 60
                        },
                        {
                            header : "任务节点名称",
                            sortable : true,
                            dataIndex : "taskName",
                            width : 60
                        },{
                            header : "任务类型",
                            sortable : true,
                            dataIndex : "taskType",
                            width : 60,
                            renderer : function(v){
                                if(v==1){
                                    return "个人任务";
                                }
                                if(v==2){
                                    return "组任务";
                                }
                            }
                        },
                        {
                            header : "流程变量",
                            sortable : true,
                            dataIndex :"variableName",
                            width : 60
                        },{
                            header : "审核人",
                            sortable : true,
                            dataIndex : "checkUserName",
                            width : 60
                        },                        
                        {
                            header : "管理",
                            dataIndex : "id",
                            sortable : true,
                            width : 60,
                            renderer : function(j) {
                                var k="";
                                        k += '<button title="删除" value=" " class="btn-delete" onclick="WorkFlowAssigneeManagerView.remove('
                                                + j + ')"></button>';
                                        k += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="WorkFlowAssigneeManagerView.edit('
                                                + j + ')"></button>';
                                return k;
                            }
                        } ],
                defaults : {
                    sortable : true,
                    menuDisabled : true,
                    width : 100
                }
            });
    var toolbar = new Ext.Toolbar({
        height : 30,
        items : []
    });
    //增加
        toolbar.add(new Ext.Button({
            text : "添加配置信息",
            iconCls : "btn-add",
            handler : function() {
                WorkFlowAssigneeManagerView.add();
            }
        }));
        toolbar.add(new Ext.Button({
        text : "删除配置信息",
        iconCls : "btn-delete",
        handler : function() {
            WorkFlowAssigneeManagerView.multiRemove();
        }
        }));
    
    
    var gridPanel = new Ext.grid.GridPanel({
        region : "center",
        id : "WorkFlowAssigneeManagerViewGrid",
        tbar :toolbar,
        height : 800,
        title : "配置信息列表",
        store : store,
        shim : true,
        trackMouseOver : true,
        disableSelection : false,
        loadMask : true,
        cm : columnModel,
        sm : checkboxSelectionModel,
        viewConfig : {
            forceFit : true,
            enableRowBody : false,
            showPreview : false
        },
        bbar : new HT.PagingBar({
            store : store
        })
    });
    gridPanel.addListener("rowdblclick", function(grid, rowIndex, columnIndex, event) {
        var record = grid.getStore().getAt(rowIndex);
        var cfg = {
            id : record.data.id,
            processId:record.data.workflowId,
            isShow : true,
            operation : Constant.Operation.VIEW
        };
       new WorkflowConfigForm(cfg).show();
    });
    var treePanel = new Ext.tree.TreePanel({
        region : "west",
        id : "WorkFlowAssigneeManagerTreePanel",
        title : "流程列表",
        collapsible : true,
        autoScroll : true,
        checkModel: "single",
        split : true,
        height : 800,
        width : 180,
        tbar : new Ext.Toolbar({
            items : [ {
                xtype : "button",
                iconCls : "btn-refresh",
                text : "刷新",
                handler : function() {
                    treePanel.root.reload();
                }
            }/*, "-", {
                xtype : "button",
                text : "展开",
                iconCls : "btn-expand",
                handler : function() {
                    g.expandAll();
                }
            }, "-", {
                xtype : "button",
                text : "收起",
                iconCls : "btn-collapse",
                handler : function() {
                    g.collapseAll();
                }
            } */]
        }),
        loader : new Ext.tree.TreeLoader({
            url : __ctxPath + "/bpmndemo/findFlowTreeWorkflow.do"
        }),
        root : new Ext.tree.AsyncTreeNode({
            expanded : true
        }),
        rootVisible : false,
        listeners : {
            "click" : WorkFlowAssigneeManagerView.clickNode,
        }
    });
/*
    var d = new Ext.FormPanel({
        id : "productionWorkFlowAssigneeManagerViewSearchPanel",
        height : 40,
        frame : false,
        border : false,
        region : "north",
        layout : "form",
        layoutConfig : {
            padding : "5px",
            align : "middle"
        },
        items : [ {
            xtype : "container",
            layout : "column",
            border : false,
            fieldLabel : "&nbsp;&nbsp;查询条件",
            style : "margin:8px 5px 10px 5px;",
            defaults : {
                xtype : "label",
                border : false,
                height : 25
            },
            layoutConfig : {
                align : "middle"
            },
            items : [ 
                 {
                    text : "工艺编号："
                 }, {
                    xtype : "textfield",
                    name : "Q_techNo_S_LK"
                }, {
                    text : "工艺名称："
                }, {
                    xtype : "textfield",
                    name : "Q_techName_S_LK"
                },{
                    text : "工艺版次："
                }, {
                    xtype : "textfield",
                    name : "Q_techVersion_S_LK"     
                }, {
                style : "margin-left: 5px;",
                xtype : "button",
                text : "搜索",
                scope : this,
                iconCls : "search",
                handler : WorkFlowAssigneeManagerView.search
            }, {
                style : "margin-left: 10px;",
                xtype : "button",
                text : "清空",
                scope : this,
                iconCls : "reset",
                handler : WorkFlowAssigneeManagerView.reset
            } ]
        } ]
    });*/

    var panel = new Ext.Panel({
        id : "WorkFlowAssigneeManagerView",
        closable : true,
        iconCls : "menu-technologyInfromation",
        layout : "border",
        items : [ gridPanel, /*d,*/ treePanel]
      /*  keys : [ {
            key : Ext.EventObject.ESC,
            fn : WorkFlowAssigneeManagerView.reset,
            scope : this
        }, {
            key : Ext.EventObject.ENTER,
            fn : WorkFlowAssigneeManagerView.search,
            scope : this
        } ]*/
    });
        Ext.applyIf(panel,cfg);
    return panel;
        
    
};
WorkFlowAssigneeManagerView.prototype.initData = function() {
    var store = new Ext.data.Store({
        proxy : new Ext.data.HttpProxy({
            url : __ctxPath + "/bpmndemo/listWorkflowConfig.do"
        }),
        reader : new Ext.data.JsonReader({
            root : "result",
            totalProperty : "totalCounts",
            fields : [ "id","companyName","departmentName","variableName","checkUserName","taskName","taskType","workflowId"]
        
        }),
        remoteSort : true,
        listeners : {
            beforeload:function(s){
                var node=Ext.getCmp("WorkFlowAssigneeManagerTreePanel").getSelectionModel().getSelectedNode();
                if(!node){
                    return false;
                }
                var myparams={
                       Q_workflowKey_S_EQ:node.id
                }
                Ext.apply(s.baseParams,myparams);
            }
        }
    });
    store.setDefaultSort("createTime", "desc");
    return store;
};
WorkFlowAssigneeManagerView.multiRemove = function() {
    var b = Ext.getCmp("WorkFlowAssigneeManagerViewGrid");
    var d = b.getSelectionModel().getSelections();
    if (d != null && d.length > 0) {
        var c = new Array();
        for ( var panel = 0; panel < d.length; panel++) {
            c.push(d[panel].data.id);
        }
        WorkFlowAssigneeManagerView.remove(c);
    } else {
        Ext.ux.Toast.msg("操作提示", "请选择你要删除的数据！");
    }
};
WorkFlowAssigneeManagerView.remove = function(ids) {
    Ext.Msg.confirm("删除操作", "确定要删除该配置信息吗?", function(b) {
        if (b == "yes") {
            Ext.Ajax.request({
                url : __ctxPath + "/bpmndemo/deleteWorkflowConfig.do",
                method : "post",
                params : {
                    ids : ids
                },
                success : function(c) {
                    var obj=Ext.decode(c.responseText); 
                    if(obj.success){
                        Ext.ux.Toast.msg("操作提示", "删除成功！");
                        Ext.getCmp("WorkFlowAssigneeManagerViewGrid").getStore().reload();
                    }else{
                        Ext.MessageBox.show({
                            title : "操作提示",
                            msg : obj.msg?obj.msg:"删除出错，请联系管理员！",
                            buttons : Ext.MessageBox.OK,
                            icon : "ext-mb-error"
                        });
                        Ext.getCmp("WorkFlowAssigneeManagerViewGrid").getStore().reload();
                    }
                },
                failure : function() { 
                    Ext.ux.Toast.msg("操作提示", "删除失败！");
                }
            });
        }
    });
};
WorkFlowAssigneeManagerView.clickNode = function(node) {
    WorkFlowAssigneeManagerView.select(node);
};
WorkFlowAssigneeManagerView.select = function(node) {
    Ext.getCmp("WorkFlowAssigneeManagerTreePanel").getSelectionModel().clearSelections();
    Ext.getCmp("WorkFlowAssigneeManagerTreePanel").getSelectionModel().select(node);
    Ext.getCmp("WorkFlowAssigneeManagerViewGrid").getStore().reload();
};
WorkFlowAssigneeManagerView.add = function(panel) {
    var selectedNode = Ext.getCmp("WorkFlowAssigneeManagerTreePanel").getSelectionModel().getSelectedNode();
    console.info(selectedNode);
    if (!selectedNode||selectedNode.id=="") {
        Ext.ux.Toast.msg("操作提示", "请先选择流程！");
        return;
    } 
    var cfg = {
            processKey:selectedNode.id,
            processName :　selectedNode.text,
            operation : Constant.Operation.ADD,
            addYearGridPanel : "WorkFlowAssigneeManagerViewGrid"
        }
    new WorkflowConfigForm(cfg).show();
};
WorkFlowAssigneeManagerView.edit = function(id) {
    Ext.Ajax.request({
        url: __ctxPath + "/bpmndemo/getWorkflowConfig.do",
        params: { id: id },
        success: function(resp,thiz){
           var rsponObj= Ext.decode(resp.responseText);
           var workflowKey=rsponObj.result.workflowKey;
           new WorkflowConfigForm({
               id:id,
               processKey:workflowKey,
               operation : Constant.Operation.EDIT,
               addYearGridPanel : "WorkFlowAssigneeManagerViewGrid"
               }).show();
        },
        failure: function(){
            
        }
     });
};

